import 'package:boss/models/staff_tip_page_data.dart';
import 'package:boss/ui/page/staffTip/provider/staff_tip_provider.dart';
import 'package:boss/ui/page/staffTip/widget/staff_tip_item.dart';
import 'package:boss/ui/widget/loader/loader_pull_refresh.dart';
import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';
import 'package:async/async.dart';
import 'package:provider/provider.dart';

class StaffTipList extends StatefulWidget {
  @override
  _StaffTipListState createState() => _StaffTipListState();
}

class _StaffTipListState extends State<StaffTipList> {
  Future<Result<List<StaffTip>>> _loadTask() async {
    var staffTipProvider = Provider.of<StaffTipProvider>(
      context,
      listen: false,
    );
    return staffTipProvider.loadMoreStaffTipList();
  }

  Future<Result<List<StaffTip>>> _refreshTask() async {
    var staffTipProvider = Provider.of<StaffTipProvider>(
      context,
      listen: false,
    );
    return staffTipProvider.refreshStaffTipList();
  }

  @override
  Widget build(BuildContext context) {
    return Expanded(
      child: Consumer<StaffTipProvider>(
        builder: (context, staffTipProvider, _) {
          return LoaderPullRefresh<StaffTip>(
            key: staffTipProvider.loaderPullRefreshKey,
            loadTask: _loadTask,
            refreshTask: _refreshTask,
            builder: (context) {
              return ListView.builder(
                padding: EdgeInsets.only(
                  left: 30.w,
                  right: 30.w,
                  bottom: 30.w,
                ),
                itemBuilder: (context, index) {
                  var current = staffTipProvider.staffTips[index];

                  return StaffTipItem(
                    data: current,
                    index: index,
                  );
                },
                itemCount: staffTipProvider.staffTips.length,
              );
            },
          );
        },
      ),
    );
  }
}
